package com.allways.dubbo.mdm.service.logiline.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.logiline.Logiline;
import com.allways.dubbo.mdm.service.logiline.ILogilineService;

/**
 * <p>ClassName: ILogilineService</p>
 * <p>Description: 物流运作区域Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-14</p>
 */
@Service("mdmLogilineService")
public class LogilineServiceImpl extends MdmBaseDaoImpl implements ILogilineService {
	Log log = LogFactory.getLog(LogilineServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addLogiline(Logiline logiline) {
   		log.debug("For the new logiline parameters:"+ logiline.toJson());
   		validColumnLen(logiline);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(logiline.getLogilineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getLogilineByCode(logiline.getLogilineCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("logiline.insertLogiline",logiline);
    }

    @Override
    public int updateLogiline(Logiline logiline) {
    	log.debug("Update for the logiline parameters:"+ logiline.toJson());
    	validColumnLen(logiline);
     	//无code的不能更新
        if(StringUtils.isEmpty(logiline.getLogilineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Logiline oldLogiline = getLogilineByCode(logiline.getLogilineCode());
        if(null == oldLogiline){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldLogiline.setId(logiline.getId());
		oldLogiline.setLogisticsAreaId(logiline.getLogisticsAreaId());
		oldLogiline.setLogisticsAreaCode(logiline.getLogisticsAreaCode());
		oldLogiline.setLogisticsAreaLineLevel(logiline.getLogisticsAreaLineLevel());
		oldLogiline.setLogisticsPointId(logiline.getLogisticsPointId());
		oldLogiline.setLogisticsPointCode(logiline.getLogisticsPointCode());
		oldLogiline.setCountryCode(logiline.getCountryCode());
		oldLogiline.setProvinceCode(logiline.getProvinceCode());
		oldLogiline.setCityCode(logiline.getCityCode());
		oldLogiline.setCountyCode(logiline.getCountyCode());
		oldLogiline.setIsDisabled(logiline.getIsDisabled());
		oldLogiline.setDataSource(logiline.getDataSource());
		oldLogiline.setCreatedBy(logiline.getCreatedBy());
		oldLogiline.setCreatedTime(logiline.getCreatedTime());
		oldLogiline.setUpdatedBy(logiline.getUpdatedBy());
		oldLogiline.setUpdatedTime(logiline.getUpdatedTime());
        
        return update("logiline.updateLogiline",oldLogiline);
    }

    @Override
    public int deleteLogiline(Logiline logiline) {
    	log.debug("Delete for the logiline parameters:"+ logiline.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(logiline.getLogilineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("logiline.deleteLogiline",logiline);
    }

    @Override
    public Logiline getLogilineByCode(String code) {
    	log.debug("Query for logiline code:" + code);
        return queryForObject("logiline.getLogilineByCode",code);
    }
    @Override
    public long getLogilineCount(Logiline logiline) {
    	log.debug("Query for logiline count parameters:" + logiline.toJson());
        return queryForObject("logiline.getLogilineCount",logiline);
    }
   @Override
    public List<Logiline> queryLogilines(Logiline logiline, int firstRow, int lastRow) {
      	log.debug("Query for logiline parameters:" + logiline.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("logiline",logiline);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("logiline.queryLogilines", param);
    }
    
    /**
     * 验证字段长度
     * @param logiline 需要验证的对象
     */
    private void validColumnLen(Logiline logiline){
		if(!StringUtils.isEmpty(logiline.getId()) && logiline.getId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiline.getLogisticsAreaId()) && logiline.getLogisticsAreaId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiline.getLogisticsAreaCode()) && logiline.getLogisticsAreaCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiline.getLogisticsAreaLineLevel()) && logiline.getLogisticsAreaLineLevel().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiline.getLogisticsPointId()) && logiline.getLogisticsPointId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiline.getLogisticsPointCode()) && logiline.getLogisticsPointCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiline.getCountryCode()) && logiline.getCountryCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(logiline.getProvinceCode()) && logiline.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiline.getCityCode()) && logiline.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiline.getCountyCode()) && logiline.getCountyCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiline.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
